package com.credithc.signature.common.service;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.constant.CacheConstant;
import com.credithc.signature.common.constant.ManagerConstant;
import com.credithc.signature.common.mapper.DictionaryMapper;
import com.credithc.signature.common.po.Dictionary;
import com.credithc.signature.common.po.Template;
import com.credithc.signature.common.ro.manager.DictionaryRo;
import com.credithc.signature.common.utils.CacheUtil;
import com.credithc.signature.common.utils.ManagerBeanUtil;
import com.credithc.signature.common.vo.manager.DictionaryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Slf4j
@Service
public class DictionaryService {

    @Autowired
    DictionaryMapper dictionaryMapper;

    /**
     * 根据code和type查询
     *
     * @param type
     * @param code
     * @return
     */
    public String selectOneByTypeAndCode(String type, String code) {
        Dictionary Dictionary = new Dictionary();
        Dictionary.setType(type);
        Dictionary.setCode(code);
        Map<String, Object> params = ManagerBeanUtil.buildQueryParams(Dictionary);
        Dictionary po = dictionaryMapper.selectOneByExample(params.get(ManagerConstant.EXAMPLE));
        if (po != null) {
            return po.getName();
        } else {
            return null;
        }
    }

    /**
     * 根据缓存查询
     *
     * @param type
     * @param code
     * @return
     */
    public String selectOneByCache(String type, String code) {
        Dictionary cacheDictionary = null;
        try {
            cacheDictionary = CacheUtil.getDictionaryLoadingCache().get(CacheConstant.DICTIONARY_PREFIX + type + CacheConstant.DICTIONARY_PREFIX + code);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return cacheDictionary.getName();
    }

    /**
     * 根据type查询
     *
     * @param type
     * @return
     */
    public List<Dictionary> selectByType(String type) {
        Dictionary Dictionary = new Dictionary();
        Dictionary.setType(type);
        Map<String, Object> params = ManagerBeanUtil.buildQueryParams(Dictionary);
        return dictionaryMapper.selectByExample(params.get(ManagerConstant.EXAMPLE));
    }

    /**
     * 根据type和扩展字段查询查询
     *
     * @param type
     * @return
     */
    public List<Dictionary> selectByTypeAndOther(String type, String ext) {
        Dictionary Dictionary = new Dictionary();
        Dictionary.setType(type);
        Dictionary.setExt1(ext);
        Map<String, Object> params = ManagerBeanUtil.buildQueryParams(Dictionary);
        return dictionaryMapper.selectByExample(params.get(ManagerConstant.EXAMPLE));
    }

    /**
     * 查询所有
     *
     * @param dictionaryRo
     * @return
     */
    public PageInfo<DictionaryVo> queryAll(DictionaryRo dictionaryRo) {
        Dictionary dictionary = new Dictionary();
        Map<String, Object> params = ManagerBeanUtil.buildQueryParams(dictionaryRo, dictionary);
        List<Dictionary> dictionaries = dictionaryMapper.selectByExampleAndRowBounds(params.get(ManagerConstant.EXAMPLE), (RowBounds) params.get(ManagerConstant.ROWBOUNDS));
        int count = dictionaryMapper.selectCountByExample(params.get(ManagerConstant.EXAMPLE));
        List<DictionaryVo> vos = buildPos(dictionaries);
        return new PageInfo<>(vos, count, dictionaryRo.getPageNo(), dictionaryRo.getPageSize());
    }


    //---------------------------------工具方法-----------------------------------

    private List<DictionaryVo> buildPos(List<Dictionary> dictionaries) {
        List<DictionaryVo> list = new ArrayList<>();
        dictionaries.forEach(po -> {
            DictionaryVo vo = new DictionaryVo();
            list.add(converterVo(po, vo));
        });
        return list;
    }

    private DictionaryVo converterVo(Dictionary po, DictionaryVo vo) {
        BeanUtils.copyProperties(po, vo);
        //启用状态
        if (po.getDictStatus() != null) {
            String name = this.selectOneByTypeAndCode(ManagerConstant.COMMON_STATUS, po.getDictStatus().toString());
            vo.setDictStatusName(name);
        }
        return vo;
    }


    /**
     * 删除
     *
     * @param id
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void remove(Long id) {
        dictionaryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改
     *
     * @param dictionaryRo
     */
    public void update(DictionaryRo dictionaryRo) {
        Dictionary dictionary = new Dictionary();
        BeanUtils.copyProperties(dictionaryRo, dictionary);
        dictionaryMapper.updateByPrimaryKeySelective(dictionary);
    }

    /**
     * 新增
     *
     * @param dictionaryRo
     */
    public void save(DictionaryRo dictionaryRo) {
        Dictionary dictionary = new Dictionary();
        BeanUtils.copyProperties(dictionaryRo, dictionary);
        dictionaryMapper.insert(dictionary);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    public DictionaryVo queryOne(Long id) {
        Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(id);
        DictionaryVo vo = new DictionaryVo();
        return converterVo(dictionary, vo);
    }

    /**
     * 新增字典数据
     *
     * @param dictionary
     */
    public void insert(Dictionary dictionary) {
        dictionaryMapper.insert(dictionary);
    }

    /**
     * 根据sealCode删除
     *
     * @param sealCode
     */
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void deleteBySealCode(String sealCode) {
        try {
            Dictionary dictionary = new Dictionary();
            dictionary.setCode(sealCode);
            Map<String, Object> params = ManagerBeanUtil.buildQueryParams(dictionary);
            dictionaryMapper.deleteByExample(params.get(ManagerConstant.EXAMPLE));
            log.info("删除Dictionary表公章信息成功......");
        } catch (Exception e) {
            log.error("删除Dictionary表公章信息失败......");
        }
    }

    public List<DictionaryVo> queryDictTypes() {
        List<Dictionary> dictTypes = dictionaryMapper.queryDictTypes();
        List<DictionaryVo> list = new ArrayList<>();
        dictTypes.forEach(po -> {
            DictionaryVo vo = new DictionaryVo();
            BeanUtils.copyProperties(po, vo);
            list.add(vo);
        });
        return list;
    }
}
